<?php

namespace app\controller;

use app\BaseController;
use app\model\Jinshu;
use app\model\Kll;
use app\model\Searchkll;
use think\Db;
use think\facade\View;

class Index extends BaseController
{
    //卡路里记录图
    public function kaluli(){
        //计算每天基础代谢量
        $time=gettime();
        //查看基础代谢量是否已经录入
        $base=new Kll();
        if (!$base->where(['type'=>1,'time'=>$time])->find()){
            $modeljinshu=new Jinshu();
            //输出当前斤数
            $jinshu=$modeljinshu->order('time desc')->where('time','<',$time)->find();
            $jinshu->jinshu;
            //男：66+（13.7*体重）+（5*身高）-（6.8*年龄）
            //女：655+（9.6*体重）+（1.7*身高）-（4.7*年龄）
            $age=(time()-strtotime('1982-2-16'))/(3600*24*365);
            $savekll['type']=1;
            $savekll['kll']=66+(13.7*($jinshu->jinshu/2))+(5*175)-(6.8*$age);
            $savekll['name']='基础消耗';
            $savekll['zl']=$jinshu->jinshu;
            $savekll['time']=$time;
            $base->insert($savekll);
        }
        //输出对应的每天基础消耗
        $kllarrtemp=[];
        for($i=$time-6;$i<=$time;$i++){
            $find=$base->order('time asc')
                ->where('time',$i)
                ->where('type',1)
                ->find();
            if($find){
                $kllarrtemp[]=$find->kll;
            }else{
                $kllarrtemp[]='';
            }
        }
        $kllarrtemp=array_reverse($kllarrtemp);
        foreach ($kllarrtemp as $k=>$v){
            if(isset($kllarrtemp[$k+1])&&$kllarrtemp[$k+1]==''){
                $kllarrtemp[$k+1]=$v;
            }
        }
        $kllarrtemp=array_reverse($kllarrtemp);
        $data['kll']=implode(',',$kllarrtemp);
        //输出对应的每天吃饭增加
        $kllarrtemp=[];
        for($i=$time-6;$i<=$time;$i++){
            $find=$base->order('time asc')
                ->where('time',$i)
                ->where('type',3)
                ->sum('kll');
            if($find){
                $kllarrtemp[]=$find;
            }else{
                $kllarrtemp[]='';
            }
        }
        $data['eat']=implode(',',$kllarrtemp);
        //输出运动消耗卡路里
        $kllarrtemp=[];
        for($i=$time-6;$i<=$time;$i++){
            $find=$base->order('time asc')
                ->where('time',$i)
                ->where('type',2)
                ->find();
            if($find){
                $kllarrtemp[]=$find->kll;
            }else{
                $kllarrtemp[]='';
            }
        }
        $data['ydkll']=implode(',',$kllarrtemp);
        //输出一周时间排序
        $zhou=explode(',',getzhou());
        foreach ($zhou as $key=>$v){
            $jsmodel=new Kll();
            $thistime=$time-6+$key;
            $thistime=$time-6+$key;
            //摄入热量
            $sheru=$jsmodel->where([['type','in',[1,2]]])
                ->where('time',$thistime)
                ->sum('kll');
            //消耗热量
            $xh=$jsmodel->where([['type','in',[3]]])
                ->where('time',$thistime)
                ->sum('kll');
//            echo $thistime .','. $sheru .','. $xh.'<br>';
//            百分比
            $bfb=(((int)$sheru-$xh)/1250*100);
            $bfb=sprintf('%.2f', $bfb).'%';
            $zhou[$key]='\''.$v.' ('.$bfb.')\'';
        }
//        p($zhou);
        $data['zhou']=implode(',',$zhou);
        //录入今日运动消耗
        $mjinshu=new Kll();
        $find=$mjinshu->where('time',gettime())
            ->where('type',2)
            ->find();
        if($find){
            $tizhong=$find->kll;
        }else{
            $tizhong='今日运动未录入';
        }
        View::assign('kll',$tizhong);
        //今日基础消耗，取最近一天的值
        $model=new Kll();
        //基础消耗
        $daybase=$model->order('time desc')->where(['time'=>gettime()])->where(['type'=>1])->value('kll');
        //运动消耗
        $dayxiaohao=$model->order('time desc')->where(['type'=>2,'time'=>gettime()])->value('kll');
        //实际摄入
        $dayeat=$model->order('time desc')->where(['type'=>3,'time'=>gettime()])->sum('kll');
        //实际消耗字符串
        $xiaohaokll=$daybase+$dayxiaohao-$dayeat-1250;
        //还可以吃
        View::assign('hkyc',$xiaohaokll);
        View::assign('data',$data);
        return View::fetch();
    }
    //录入食物信息
    public function lrshiwu(){
        if($this->request->post()){
            $post=$this->request->post();
            $model=new Searchkll();
            foreach ($post as $key=>$v){
                $model->$key=$v;
            }
            $model->time=time();
            $model->save();
        }
        return redirect('/index/srkll');
    }
    //ajax获取食物名称
    public function getshiwuname($name=''){
        $model=new Searchkll();
        $arr=$model
            ->where([['name','like','%'.$name.'%']])
            ->whereOr([['pinyin','like','%'.$name.'%']])
            ->order('time desc')
            ->limit(50)
            ->select()->toArray();
        foreach ($arr as $key=>$v){
            if($v['reliang']<=100){
                $arr[$key]['dengji']='list-group-item-success';
                $arr[$key]['name2']=$v['name'].'(低)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
            if($v['reliang']>100&&$v['reliang']<=150){
                $arr[$key]['dengji']='list-group-item-primary';
                $arr[$key]['name2']=$v['name'].'(中)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
            if($v['reliang']>150&&$v['reliang']<=200){
                $arr[$key]['dengji']='list-group-item-danger';
                $arr[$key]['name2']=$v['name'].'(高)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
            if($v['reliang']>200){
                $arr[$key]['dengji']='list-group-item-dark';
                $arr[$key]['name2']=$v['name'].'(极高)'.'  '.' '.$v['reliang'].' kcal/100g';
            }
        }
        return json_encode($arr);
    }
    //输入摄入卡路里
    public function srkll(){
        if($post=$this->request->post()){
            $modelkll=new Kll();
            $modelkll->type=$post['type'];
            $modelkll->kll=$post['reliang']*$post['zl']/100;
            $modelkll->name=$post['name'];
            $modelkll->zl=$post['zl'];
            $modelkll->time=gettime();
            $modelkll->save();
            $modelsw=new Searchkll();
            $modelsw->where('name',$post['name'])
                ->update(['time'=>time()]);
        }
        $model=new Searchkll();
        $arr=$model->order('time desc')
            ->limit(50)
            ->select()
            ->toArray();
        View::assign('arr',$arr);
        //输出今日体重
        $mjinshu=new Jinshu();
        $find=$mjinshu->where('time',gettime())
            ->find();
        if($find){
            $tizhong=$find->jinshu;
        }else{
            $tizhong='今日体重未录入';
        }
        View::assign('tz',$tizhong);
        //录入今日运动消耗
        $mjinshu=new Kll();
        $find=$mjinshu->where('time',gettime())
            ->where('type',2)
            ->find();
        if($find){
            $tizhong=$find->kll;
        }else{
            $tizhong='今日运动未录入';
        }
        View::assign('kll',$tizhong);
        /*减肥目标和今日差距
        1kg热量值是7500大卡
        */
        $kg=(220-160)/2;
        $zhongyao['kg']=$kg;
        //总消耗千卡
        $zkll=$kg*7500;
        $zhongyao['zkll']=$zkll;
        //平均每天消耗卡路里
        $daykll=$zkll/180;
        $zhongyao['daykll']=$daykll;
        //今日基础消耗，取最近一天的值
        $model=new Kll();
        //基础消耗
        $daybase=$model->order('time desc')->where(['time'=>gettime()])->where(['type'=>1])->value('kll');
        $zhongyao['daybase']=$daybase;
        //运动消耗
        $dayxiaohao=$model->order('time desc')->where(['type'=>2,'time'=>gettime()])->value('kll');
        $zhongyao['dayxiaohao']=$dayxiaohao;
        //实际摄入
        $dayeat=$model->order('time desc')->where(['type'=>3,'time'=>gettime()])->sum('kll');
        $zhongyao['dayeat']=$dayeat;
        //实际消耗字符串
        $xiaohaokll='基础消耗（'.$daybase.')+运动消耗('.$dayxiaohao.')-摄入热量('.$dayeat.')='.($daybase+$dayxiaohao-$dayeat).'千卡';
        $zhongyao['xiaohaokll']=$xiaohaokll;
        //实际消耗
        $sjxh=$daybase+$dayxiaohao-$dayeat;
        $zhongyao['sjxh']=$sjxh;
        //每天减重
        $dayjz=$daykll/7500*2;
        $zhongyao['dayjz']=$dayjz;
        //今日完成比例
        $wanchengbl=($daybase+$dayxiaohao-$dayeat)/$daykll*100;
        $zhongyao['wanchengbl']=$wanchengbl;
        //今日摄入食物
        $eatlist=$model->where(['type'=>3,'time'=>gettime()])->order('kll desc')
            ->select()->toArray();
        foreach ($eatlist as $k=>$v){
            $eatlist[$k]['hebing']=$v['name'].$v['zl'].'克'.$v['kll'].'大卡';
        }
        $zhongyao['eatlist']=$eatlist;
        //相当于慢跑多长时间
        
        View::assign('zhongyao',$zhongyao);
        return View::fetch();
    }
    //体重燃尽图
    public function index()
    {
        //输出今日体重
        $mjinshu=new Jinshu();
        $find=$mjinshu->where('time',gettime())
            ->find();
        if($find){
            $tizhong=$find->jinshu;
        }else{
            $tizhong='今日体重未录入';
        }
        View::assign('tz',$tizhong);
        $modeljinshu=new Jinshu();
        $all=$modeljinshu->order('time asc')->select();
        $list[]=220;
        foreach ($all as $v){
            $list[]=$v->jinshu;
        }
        $meitianshou=(215-160)/181;
        for($i=1;$i<182.5;$i++){
            $di[]=$i;
            $yz[]=215-($meitianshou*$i)+$meitianshou;
        }
        foreach ($yz as $key=>$v){
            if(!isset($list[$key])){
//                $list[$key]=$v;
            }
        }
        $list=implode(',',$list);
        View::assign('list',$list);
        return View::fetch();
    }

    //录入斤数
    public function luru($jinshu=0){
        if($jinshu==''||$jinshu==0||$jinshu>300||$jinshu<160){
            echo '<div style="text-align:center;width: 900px;font-size:80px; margin: 200px auto">请输入今天的体重</div>';die;
        }
        $modeljinshu=new Jinshu();
        $time=gettime();
        //若以前有没录入的，按平均值录入
        $lasttime=$modeljinshu
            ->order('time desc')
            ->find();
        while($lasttime->time+1<$time){
            $tempjinshu=($jinshu-$lasttime->jinshu)/($time-$lasttime->time);
            $modeljinshu
                ->insert(['jinshu'=>$lasttime->jinshu+$tempjinshu,'time'=>$lasttime->time+1]);
            $lasttime=$modeljinshu
                ->order('time desc')
                ->find();
        }
        //若以前有没录入的，按平均值录入 完成
        $find=$modeljinshu
            ->where('time',$time)
            ->find();
        if($find){
            $find->jinshu=$jinshu;
            $find->save();
        }else{
            $modeljinshu
                ->insert(['jinshu'=>$jinshu,'time'=>$time]);
        }
        return redirect('/a');
    }
    //录入消耗卡路里
    public function lurukll($iwatch=0){
//        if($iwatch==''||$iwatch==0||$iwatch>3000||$iwatch<50){
//            echo '<div style="text-align:center;width: 900px;font-size:80px; margin: 200px auto">请输入今天的消耗</div>';die;
//        }
        $modelkll=new Kll();
        $time=gettime();
        $find=$modelkll
            ->where(['time'=>$time,'type'=>2])
            ->find();
        if($find){
            $find->kll=$iwatch;
            $find->save();
        }else{
            $modelkll
                ->insert(['kll'=>$iwatch,'type'=>2,'name'=>'运动消耗','zl'=>0,'time'=>$time]);
        }
        return redirect('/b');
    }

    public function getpinyin($name=0){
        $modelsearchkll=new Searchkll();
        $find=$modelsearchkll->where('name',$name)
            ->find();
        if($find) return '食物已经录入过了';
        return pinyin($name);
    }

    public function hello($name = 'ThinkPHP8')
    {
        return 'hello,' . $name;
    }
}
